package com.zxjbyte.yiyi.framework.common.core.eventbus;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import lombok.Data;
import lombok.Getter;

import java.util.concurrent.Executor;

/**
 * 异步事件总线服务
 *
 * @Author zhangxingjia
 * @Date 2025/3/31 15:24
 * @Version: 1.0
 */
@Getter
public class AsyncEventBusService implements EventDispatcher{

    private EventBus eventBus;

    private AsyncEventBusService() {
    }

    /**
     * 创建异步事件总线，根据executor分发事件
     * @param executor 线程池
     * @return
     */
    public static AsyncEventBusService asyncService(Executor executor) {
        AsyncEventBusService asyncEventBusService = new AsyncEventBusService();
        asyncEventBusService.eventBus = new AsyncEventBus(executor);
        return asyncEventBusService;
    }

    @Override
    public void register(Object subscriber) {
        eventBus.register(subscriber);
    }

    @Override
    public void unregister(Object subscriber) {
        eventBus.unregister(subscriber);
    }

    @Override
    public void post(Object event) {
        eventBus.post(event);
    }
}
